﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class StudentEdit : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            if (Request.QueryString["XH"] != null)
            {
                this.LoadData();
            }
        }

        private void LoadData()
        {
            string XH = Request.QueryString["XH"].ToString();
            xy_xsxx md = xy_xsxx.Where("XH=@0", XH).First();
            this.txt_XH.Text = md.XH;
            this.txt_CARDID.Text = md.CARDID.ToString();
            this.txt_XJZT.Text = md.XJZT;
            this.txt_XL.Text = md.XL;
            this.txt_XM.Text = md.XM;
            this.Drop_Sex.SelectedValue = md.XB;
            this.SelectBJ.DeptID = md.BJDM;
            this.SelectBJ.DeptName = md.XZB;
        }

        private bool IsExistsXH(string XH)
        {
            return xy_xsxx.Where("XH=@0", XH).Count() > 0;
        }

        private void AddStudent()
        {
            if (IsExistsXH(this.txt_XH.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('该学号已存在！'); });</script>");
                return;
            }

            if (this.SelectBJ.DeptID == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('请选择所属班级！'); });</script>");
                return;
            }

            xy_xsxx md = new xy_xsxx();
            md.XH = this.txt_XH.Text;
            md.CARDID = Convert.ToInt32(this.txt_CARDID.Text.Trim() == "" ? "0" : this.txt_CARDID.Text.Trim());
            md.XJZT = this.txt_XJZT.Text;
            md.XL = this.txt_XL.Text;
            md.XM = this.txt_XM.Text;
            md.XB = this.Drop_Sex.SelectedValue;

            xy_bj bj = xy_bj.Where("BJDM=@0",this.SelectBJ.DeptID).FirstOrDefault();
            if (bj != null)
            {
                md.NJ = Convert.ToInt32(bj.NJ);
                md.XZ = bj.XZ;
                md.ZYDM = bj.SSZYDM;
                md.ZYMC = bj.SSZYMC;
                md.XZB = bj.BJDM;//bj.BJMC;  因为原始数据 里面 BMDM 是为空的。。
                md.BJDM = bj.BJDM;
                md.XY = bj.XYMC;
                md.XYDM = bj.XYDM;
            }

            md.IdKey = Guid.NewGuid();
            md.IsEdit = false;
            xy_xsxx.Insert(md);

            UserList umd = new UserList();
            umd.XH = md.XH;
            umd.IsStudent = true;
            umd.UserName = md.XH;
            umd.PassWord = Utility.GetMD5PassWord(md.XH, "");
            umd.UName = md.XM;
            umd.DataRole = 13;//学生管理员
            umd.RoleCode = "2e8ad540e9889cfb";
            umd.RoleName = "学生";
            umd.AddTime = DateTime.Now;
            UserList.Insert(umd);
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');window.parent.jsRefreshList();</script>");
        }

        private void EditStudent()
        {
            string XH = Request.QueryString["XH"].ToString();
            if (this.txt_XH.Text.Trim() != XH)
            {
                if (IsExistsXH(this.txt_XH.Text.Trim()))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('该学号已存在！'); });</script>");
                    return;
                }
            }

            xy_xsxx md = xy_xsxx.Where("XH=@0", XH).First();
            md.XH = this.txt_XH.Text;
            md.CARDID = Convert.ToInt32(this.txt_CARDID.Text.Trim() == "" ? "0" : this.txt_CARDID.Text.Trim());
            md.XJZT = this.txt_XJZT.Text;
            md.XL = this.txt_XL.Text;
            md.XM = this.txt_XM.Text;
            md.XB = this.Drop_Sex.SelectedValue;

            xy_bj bj = xy_bj.Where("BJDM=@0", this.SelectBJ.DeptID).FirstOrDefault();
            if (bj != null)
            {
                md.NJ = Convert.ToInt32(bj.NJ);
                md.XZ = bj.XZ;
                md.ZYDM = bj.SSZYDM;
                md.ZYMC = bj.SSZYMC;
                if (md.XZB != bj.BJMC) //表示该学生的班级被修改过
                {
                    md.IsEdit = true;
                }
                md.XZB = bj.BJDM;//bj.BJMC;  因为原始数据 里面 BMDM 是为空的。。
                md.BJDM = bj.BJDM;
                md.XY = bj.XYMC;
                md.XYDM = bj.XYDM;
            }
            xy_xsxx.Update(md, "XH=@0", XH);

            UserList Umd = UserList.Where("XH=@0 and IsStudent=@1", XH, true).FirstOrDefault();
            if (Umd != null)
            {
                if (this.txt_XH.Text.Trim() != XH)
                {
                    Umd.XH = md.XH;
                    Umd.UserName = md.XH;
                    Umd.AddTime = DateTime.Now;
                    UserList.Update(Umd, "id=@0", Umd.id);
                }
            }
            else
            {
                UserList umd = new UserList();
                umd.XH = md.XH;
                umd.IsStudent = true;
                umd.UserName = md.XH;
                umd.PassWord = Utility.GetMD5PassWord(md.XH, "");
                umd.UName = md.XM;
                umd.DataRole = 13;//学生管理员
                umd.RoleCode = "2e8ad540e9889cfb";
                umd.RoleName = "学生";
                umd.AddTime = DateTime.Now;
                UserList.Insert(umd);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');window.parent.jsRefreshList();</script>");
        }

        protected void Btn_SaveStudent_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["XH"] != null)
            {
                this.EditStudent();
            }
            else
            {
                this.AddStudent();
            }
        }

    }
}